package com.blog.controller;

import com.blog.pojo.Article;
import com.blog.pojo.ArticleComment;
import com.blog.pojo.PageResult;
import com.blog.service.ArticleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: VE
 * @DATE: 2022/10/11 15:18
 * 描述:
 */
@RestController
@RequestMapping("article")
public class ArticleController {
    private Logger logger = LoggerFactory.getLogger(ArticleController.class);
    @Autowired
    private ArticleService articleService;

    @GetMapping("test1")
    public String test1(){
        return "1111";
    }

    @GetMapping("findAllArticle")
    public PageResult findAllArticle(){
        List<Article> articleList = articleService.findAllArticle();
        for (Article a:articleList) {
            System.out.println("a = " + a.getTitle());
        }

        return PageResult.success("查询成功",articleList);
    }

    @GetMapping("getArticleById")
    public PageResult getArticleById(Integer aid){
        logger.info("根据编号查询文章详情"+aid);
        Article article = articleService.load(aid);
        List<ArticleComment> commentList = articleService.findAllCommentByAid(aid);
        Map<String,Object> resuMap = new HashMap<>();
        resuMap.put("article",article);
        resuMap.put("commentList",commentList);
        return PageResult.success("查询成功",resuMap);
    }

    @GetMapping("loadArticle")
    public PageResult loadArticle(@CookieValue(required = false,value = "uid") Integer uid){
        logger.info("根据用户查看文章" + uid);
        List<Article> articleList = articleService.findArticleByUid(uid);
        Map<String,Object> resuMap = new HashMap<>();
        resuMap.put("articleList",articleList);
        resuMap.put("articleNum",articleList.size());
        return PageResult.success("查询成功",resuMap);
    }
}
